package util

import (
	"bytes"
	"encoding/json"
	"net/http"
)

func Post[T any](url string, postData any) (*T, error) {
	var err error
	// 将数据编码为JSON
	jsonData, err := json.Marshal(postData)
	if err != nil {
		return nil, err
	}
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		// logger.Println("网络错误:", err)
		return nil, err
	}
	defer resp.Body.Close()

	var result T
	// log.Println("POST", url, string(jsonData))
	if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
		return nil, err
	}
	return &result, nil
}
